﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using ZX.WebApp.CrmWorkFlowService;

namespace ZX.WebApp.Crm.BasePages
{
    public partial class Affix1 : BaseUserControl
    {
        public string workinsid="";
        public string worktasid="";
     
        ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient client = new ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            //base.Page_Load();
            if (!IsPostBack)
            {
                InitData();
                Session["workinsid"] = Guid.NewGuid().ToString();
                Session["worktasid"] = Guid.NewGuid().ToString();
            }
        }
        private void InitData()
        {
            DataTable dt = client.GetAffixTable(Convert.ToString(Session["workinsid"]),false);
            if (dt.Rows.Count > 0)
            {
                gvAffixfiles.DataSource = dt;
                gvAffixfiles.DataBind();
            }
            return;
        }
        protected void ButtonConfirm_Click(object sender, EventArgs e)
        {
            //检查上传文件不为空

            if (inputFile.PostedFile.ContentLength > 0)
            {
                //设定上传文件的保存路径
                string strSaveDir = "../Files/";
                string strName = this.inputFile.PostedFile.FileName;
                //取得文件名(抱括路径)里最后一个"."的索引
                int intExt = strName.LastIndexOf(".");
                //取得文件扩展名
                string strExt = strName.Substring(intExt);
                //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复

                //取得文件名(包括路径)里最后一个"\"的索引
                int intPath = strName.LastIndexOf("\\");
                //取得文件名(不包括路径)
                string strNewName = strName.Substring(intPath + 1);
                string filenameid = Guid.NewGuid().ToString();
                //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
                //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
                string filenamepath = Server.MapPath(strSaveDir + filenameid + strExt);
                this.inputFile.PostedFile.SaveAs(filenamepath);
                //得到这个文件的相关属性:文件名,文件类型,文件大小
                labelUpResult.Text = "上传成功！";
                //labelFileName.Text = "文件源：" + strName;
                //labelFileExt.Text = "文件类型：" + inputFile.PostedFile.ContentType + "( " + strExt + " )";
                //labelFileSize.Text = "文件大小：" + (inputFile.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
                CrmWorkFlowService.AffixFiles entity = new CrmWorkFlowService.AffixFiles();
                entity.AffixId = filenameid;
                entity.WorkflowId = Request["WorkflowId"].ToString();
                entity.WorkflowInsId = Convert.ToString(Session["workinsid"]);
                entity.WorktaskId = Request["WorktaskId"].ToString();
                entity.WorktaskInsId = Convert.ToString(Session["worktasid"]);
                entity.FileNameId = filenameid + strExt;
                entity.FileName = strNewName;
                entity.FilePath = filenamepath;
                client.InsertAffixcrm(entity);
                labelUpResult.Text = "上传成功！";
                InitData();
                return;

            }
        }
        public static void OpenWindowInNewPage(Page curPage, string destUrl)
        {
            string scriptString = string.Format("<script language='JavaScript'>window.open('" + "{0}" + "','_new');<", destUrl);
            scriptString += "/";
            scriptString += "script>";
            if (!curPage.IsStartupScriptRegistered("Startup"))
            {
                curPage.RegisterStartupScript("Startup", scriptString);
            }
            return;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string parms = (sender as LinkButton).CommandArgument.ToString();
            OpenWindowInNewPage(this.Page, @"../Files/" + parms);
            return;
        }
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            if (CtrlState == WorkFlowConst.STATE_VIEW)
                return;
            string[] parms = (sender as LinkButton).CommandArgument.Split(',');
            client.DeleteAffix(parms[0]);//删除数据库记录文件
            File.Delete(parms[1]);
            InitData();

        }
    }
}